﻿#include "productpicker.h"
#include "ui_productpicker.h"
#include "company.h"
#include "QMessageBox"

ProductPicker::ProductPicker(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProductPicker)
{
    ui->setupUi(this);

    ui->labelDefaultCount->hide();
    ui->lineEditDefaultCount->hide();

    setWindowTitle("选择商品");
    ui->tableViewProduct->setSelectionBehavior(QAbstractItemView::SelectRows);
    setSelectionMode(QAbstractItemView::MultiSelection);//设置默认选择模式为多选
    newProductWidget.setWidgetType(SubmitProductWidget::NewProductWidget);

    connect(ui->treeViewProduct, &ProductTreeView::queryProducts,
            [=]()
    {
        productService.queryProducts(
                    ui->tableViewProduct,
                    ui->treeViewProduct,
                    ui->comboBox->currentIndex(),
                    ui->lineEditProductOverallQuery->text(),
                    ui->lineEditProductTypeQuery->text(),
                    ui->lineEditProductNameQuery->text());
        ui->tableViewProduct->hideColumn(2);
        ui->tableViewProduct->hideColumn(4);
    });

    connect(ui->tableViewProduct, &QTableView::doubleClicked,
            [=](QModelIndex index)
    {
        Q_UNUSED(index);
        this->on_pushButtonSelectReturn_clicked();
    });
}

ProductPicker::~ProductPicker()
{
    delete ui;
}

void ProductPicker::setSelectionMode(QAbstractItemView::SelectionMode selectionMode)
{
    this->selectionMode = selectionMode;
    ui->tableViewProduct->setSelectionMode(selectionMode);
}

void ProductPicker::showAndInit()
{
    ui->treeViewProduct->initWithCategory(Company::getProductCategory(), "商品类别", "所有商品");
    ui->tableViewProduct->clear();
    ui->tableViewProduct->loadProucts();
    ui->tableViewProduct->clearSelection();
    ui->lineEditDefaultCount->setText("");
    ui->tableViewProduct->hideColumn(2);
    ui->tableViewProduct->hideColumn(4);
    exec();
}

void ProductPicker::on_lineEditProductOverallQuery_textChanged(const QString &text)
{
    productService.queryProducts(
                ui->tableViewProduct,
                ui->treeViewProduct,
                ui->comboBox->currentIndex(),
                text,
                ui->lineEditProductTypeQuery->text(),
                ui->lineEditProductNameQuery->text());
    ui->tableViewProduct->hideColumn(2);
    ui->tableViewProduct->hideColumn(4);
}

void ProductPicker::on_lineEditProductTypeQuery_textChanged(const QString &text)
{
    productService.queryProducts(
                ui->tableViewProduct,
                ui->treeViewProduct,
                ui->comboBox->currentIndex(),
                ui->lineEditProductOverallQuery->text(),
                text,
                ui->lineEditProductNameQuery->text());
    ui->tableViewProduct->hideColumn(2);
    ui->tableViewProduct->hideColumn(4);
}

void ProductPicker::on_lineEditProductNameQuery_textChanged(const QString &text)
{
    productService.queryProducts(
                ui->tableViewProduct,
                ui->treeViewProduct,
                ui->comboBox->currentIndex(),
                ui->lineEditProductOverallQuery->text(),
                ui->lineEditProductTypeQuery->text(),
                text);
    ui->tableViewProduct->hideColumn(2);
    ui->tableViewProduct->hideColumn(4);
}

void ProductPicker::on_pushButtonProductClearQuery_clicked()
{
    ui->lineEditProductOverallQuery->clear();
    ui->lineEditProductTypeQuery->clear();
    ui->lineEditProductNameQuery->clear();
    ui->tableViewProduct->clearSelection();
}

void ProductPicker::on_pushButtonUpdateProductData_clicked()
{
    ui->tableViewProduct->clear();
    productService.updateProductData(this, ui->tableViewProduct);
}

void ProductPicker::on_pushButtonCancel_clicked()
{
    this->close();
}

void ProductPicker::on_pushButtonNewProduct_clicked()
{
    newProductWidget.setDirectory(ui->treeViewProduct->primaryDirectoryList(), ui->treeViewProduct->secondaryDirectoryList());
    newProductWidget.setCurrentDirectory(ui->treeViewProduct->selectedPrimaryDirectory(), ui->treeViewProduct->selectedSecondaryDirectory());
    newProductWidget.showAndClear();
    productService.updateProductData(this, ui->tableViewProduct);
}

void ProductPicker::on_pushButtonSelectReturn_clicked()
{
    QList<Product*> selectedProducts = ui->tableViewProduct->selectedProducts();

    if(selectedProducts.size() == 0){
        QMessageBox::warning(this, "警告","您还未选择商品");
    }else{
        this->close();
        int defaultCount = ui->lineEditDefaultCount->text().toInt();
        if(selectionMode == QAbstractItemView::SingleSelection){
            emit singleSelected(ui->tableViewProduct->currentProduct(), defaultCount);
        }else{
            emit selectedFinished(selectedProducts, defaultCount);
        }
    }
}

void ProductPicker::on_comboBox_currentIndexChanged(int index)
{
    productService.queryProducts(
                ui->tableViewProduct,
                ui->treeViewProduct,
                index,
                ui->lineEditProductOverallQuery->text(),
                ui->lineEditProductTypeQuery->text(),
                ui->lineEditProductNameQuery->text());
    ui->tableViewProduct->hideColumn(2);
    ui->tableViewProduct->hideColumn(4);
}
